package com.easycms.web.system.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easycms.common.CommonUtil;
import com.easycms.common.IdHelper;
import com.easycms.framework.domain.DataTableDto;
import com.easycms.framework.domain.KeyValueInt64Dto;
import com.easycms.framework.shiro.UserOwnInfo;
import com.easycms.web.system.domain.SysDictionary;
import com.easycms.web.system.domain.SysRole;
import com.easycms.web.system.domain.SysUser;
import com.easycms.web.system.mapper.SysDictionaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author yushuo
 * @className
 * @descripton TODO
 * @date 2021/2/24 16:17
 **/

@Service
public class SysDictionaryService {

    @Autowired
    private SysDictionaryMapper dictMapper;


    /**
    *
     * @author yushuo
     * @description 获取列表
     * @date 16:22 2021/2/24
     * @param [dto, code, name]
     * @return com.easycms.framework.domain.DataTableDto
    **/
    public DataTableDto getList(DataTableDto dto,String code,String name){
        LambdaQueryWrapper<SysDictionary> query=new LambdaQueryWrapper<>();
        query.eq(SysDictionary::getParentId,0);
        if(!StrUtil.isEmpty(code)){
            query.eq(SysDictionary::getDicCode,code.trim());
        }
        if(!StrUtil.isEmpty(name)){
            query.eq(SysDictionary::getDicName,name.trim());
        }

        //分页查询
        query.orderByAsc(SysDictionary::getSortNum);
        Page<SysDictionary> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        IPage<SysDictionary> userIPage = dictMapper.selectPage(page, query);
        List<SysDictionary> userList=userIPage.getRecords();

        dto.setTotal(userIPage.getTotal());
        dto.setRows(userList);
        return dto;
    }

    /**
    *
     * @author 获取数据
     * @description //TODO
     * @date 10:20 2021/2/25
     * @param [id]
     * @return com.easycms.web.system.domain.SysDictionary
    **/
    public SysDictionary getModel(long id){
        return dictMapper.selectById(id);
    }

    /**
    *
     * @author yushuo
     * @description 保存数据
     * @date 10:10 2021/2/25
     * @param [dto, userOwnInfo]
     * @return void
    **/
    public void saveDic(SysDictionary dto, UserOwnInfo userOwnInfo){
        dto.setDicValue(dto.getDicName());
        dto.setCreatePerson(userOwnInfo.getLoginName());
        dto.setCreateTime(DateUtil.date());

        if(dto.getId()==0){
            dto.setId(IdHelper.nextId());
            dictMapper.insert(dto);
            return;
        }

        dictMapper.updateById(dto);
    }

    /**
     *
     * @author yushuo
     * @description //删除数据
     * @date 15:39 2021/1/29
     * @param
     * @return void
     **/
    public void removeAll(String ids){
        List<Long> idArray = CommonUtil.toLongList(ids);
        dictMapper.deleteBatchIds(idArray);

        //删除子项
        LambdaQueryWrapper<SysDictionary> query=new LambdaQueryWrapper<>();
        query.in(SysDictionary::getParentId,idArray);
        dictMapper.delete(query);
    }



    /**
    *
     * @author yushuo
     * @description 删除子项
     * @date 10:27 2021/2/25
     * @param [ids]
     * @return void
    **/
    public void removeSonAll(String ids){
        List<Long> idArray = CommonUtil.toLongList(ids);
        dictMapper.deleteBatchIds(idArray);
    }
    /**
     *
     * @author yushuo
     * @description 获取字典项子数据列表
     * @date 16:22 2021/2/24
     * @param [dto, code, name]
     * @return com.easycms.framework.domain.DataTableDto
     **/
    public DataTableDto getSonList(DataTableDto dto,Long parentId,String name){
        LambdaQueryWrapper<SysDictionary> query=new LambdaQueryWrapper<>();
        query.eq(SysDictionary::getParentId,parentId);

        if(!StrUtil.isEmpty(name)){
            query.eq(SysDictionary::getDicName,name.trim());
        }

        //分页查询
        query.orderByAsc(SysDictionary::getSortNum);
        Page<SysDictionary> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        IPage<SysDictionary> userIPage = dictMapper.selectPage(page, query);
        List<SysDictionary> userList=userIPage.getRecords();

        dto.setTotal(userIPage.getTotal());
        dto.setRows(userList);
        return dto;
    }

    /**
    *
     * @author yushuo
     * @description 获取下拉数据
     * @date 11:36 2021/2/25
     * @param []
     * @return java.util.List<com.easycms.framework.domain.KeyValueInt64Dto>
    **/
    public List<KeyValueInt64Dto> getDictSelect(){
        LambdaQueryWrapper<SysDictionary> query=new LambdaQueryWrapper<>();
        query.eq(SysDictionary::getParentId,0);
        query.orderByAsc(SysDictionary::getSortNum);
        List<SysDictionary> list= dictMapper.selectList(query);
        List<KeyValueInt64Dto> data=new ArrayList<>();
        list.stream().forEach(s->{
            KeyValueInt64Dto dto=new KeyValueInt64Dto();
            dto.setId(s.getId());
            dto.setName(s.getDicName());
            data.add(dto);
        });

        return data;
    }
}
